package view.projectTeam;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import dal.project_team.ProjectTeam;
import dal.project_team.ProjectTeamUtil;

/**
 * Servlet implementation class AddProjectTeam
 */
@WebServlet("/AddProjectTeam")
public class AddProjectTeam extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddProjectTeam() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter printWriter=response.getWriter();
		response.setContentType("text/HTML");
		
		response.setHeader("Cache-control", "no-cache, no-store");
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Expires", "-1");
		
		response.setHeader("Access-Control-Allow-Origins", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		response.setHeader("Access-Control-Allow-Headers", "Content-Type");
	    response.setHeader("Access-Control-Max-Age", "86400");
		
	   
		JsonObject myJsonObj=new JsonObject();
		String statusText="";
		String teamId="";
		
		try {
			
			String TeamName=request.getParameter("TeamName");
			String ProjectId=request.getParameter("project_id");
			String ProjectName=request.getParameter("ProjectName");
			
			
			System.out.println("From servlet"+ProjectId+""+ProjectName+""+TeamName);
			
			
			ProjectTeam projectTeam=new ProjectTeam();
			ProjectTeamUtil projectTeamUtil=new ProjectTeamUtil();
			
			String TeamId=projectTeamUtil.getId();
			
			projectTeam.setTeam_id(TeamId);
			projectTeam.setTeam_name(TeamName);
			projectTeam.setProject_id(ProjectId);
			projectTeam.setProject_name(ProjectName);
			
			teamId=projectTeam.getTeam_id();
			
			Boolean isOk=projectTeamUtil.add(projectTeam);
			
			if(isOk==true){
				myJsonObj.addProperty("success", true);
				
				statusText= "<b>"
						+ " Project Team Information Added Succesfully "
						+ "</b>";
			}
			else{
				myJsonObj.addProperty("success", false);
				statusText= "<b>"
						+ " Project Team Information Not Added Succesfully "
						+ "</b>";
			}
			
			
		} catch (Exception e) {
			myJsonObj.addProperty("success", false);
			statusText= "<b>"
					+ " Project Team Information Not Added Succesfully "
					+ "</b>";
			e.printStackTrace();
			// TODO: handle exception
		}
		
		myJsonObj.addProperty("statusText", statusText);
		myJsonObj.addProperty("teamId", teamId);
		
		
		System.out.println(teamId);
		System.out.println(myJsonObj);
		
		
		printWriter.println(myJsonObj.toString());
		printWriter.close();
		
		// TODO Auto-generated method stub
		
	}

}
